package com.itheima.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.Book;
import org.apache.logging.log4j.util.Strings;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class BookDaoTestCase {

    @Autowired
    private BookDao bookDao;

//    @Test
//    void testGetById(){
//        System.out.println(bookDao.getById(1));
//    }

    @Test
    void testGetById( ){
        System.out.println(bookDao.selectById(1));
    }

    @Test
    void testSave(){
        Book book = new Book();
        book.setType("体育类");
        book.setName("太极拳");
        book.setDescription("左冲拳...");
        bookDao.insert(book);
    }
    @Test
    void testUpdate(){
        Book book = new Book();
        book.setId(4);
        book.setType("体育类");
        book.setName("太极拳");
        book.setDescription("左冲拳...");
        bookDao.updateById(book);
    }

    @Test
    void testDelete(){
        bookDao.deleteById(1);
    }

    @Test
    void testGetAll(){
        System.out.println(bookDao.selectList(null));;
    }

    @Test
    void testGetPage(){
        //（当前的页数，显示五条）
        IPage page = new Page(2,5);
        bookDao.selectPage(page,null);
        System.out.println(page.getCurrent());//（当前页码值）2
        System.out.println(page.getSize());//（当前页大小）5
        System.out.println(page.getTotal());//（一共多少条记录）8
        System.out.println(page.getPages());//（最大页码值）2

        System.out.println(page.getRecords());//显示你查询的记录
        // [Book(id=7, type=体育类, name=太极拳, description=左冲拳...),
        // Book(id=8, type=美术类, name=空间培养, description=空间感),
        // Book(id=9, type=心理类, name=心理学, description=墨菲定律)]
    }

    @Test
    void testGetBy(){
        QueryWrapper<Book> qw = new QueryWrapper<>();
        // select * from t_book where name like '%Spring%';
        //SELECT id,type,name,description FROM t_book WHERE (name LIKE ?)
        qw.like("name","Spring"); //"name"属性写错了怎么办？引入下一个例子
        //按条件查询
        bookDao.selectList(qw);
    }

    @Test
    void testGetBy2(){
        String a = null;
        LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<Book>();
        lqw.like(Book::getName,"Spring");//这个是语法级的，会帮你做检查
        //可以使用条件来判断
        lqw.like(a!=null,Book::getName,a);//a不等于null才连接like
        lqw.like(Strings.isNotEmpty(a),Book::getName,a);//a不为空串才连接like
        bookDao.selectList(lqw);
    }

}
